package com.changgou.order.service.impl;

import com.changgou.goods.feign.SkuFeign;
import com.changgou.goods.feign.SpuFeign;
import com.changgou.goods.pojo.Sku;
import com.changgou.goods.pojo.Spu;
import com.changgou.order.pojo.OrderItem;
import com.changgou.order.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class CartServiceImpl implements CartService {
    //注入redisTemplate连接redis
    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private SkuFeign skuFeign;
    @Autowired
    private SpuFeign spuFeign;

    public static final String CART = "cart_";

    //添加购物车
    @Override
    public void addCart(String skuId, Integer num, String username) {
        //1.查询redis中相对应的商品信息
        OrderItem orderItem = (OrderItem) redisTemplate.boundHashOps(CART + username).get(skuId);
        //2.如果当的商品在redis存在,则更新商品的数量
        if (orderItem != null) {
            orderItem.setNum(orderItem.getNum() + num); //更新库存信息
            //如果当用户删除库存信息为 <=0时,清除redis中的数据
            if (orderItem.getNum() <= 0) {
                //删除该数据
                redisTemplate.boundHashOps(CART + username).delete(skuId);//根据skuId 删除sku的信息
                return;
            }
            orderItem.setMoney(orderItem.getMoney() * orderItem.getPrice());//更新总金额
            orderItem.setPayMoney(orderItem.getMoney() * orderItem.getPrice()); //更新支付金额
        } else {
            //3.如果当前商品的信息在redis中不存在，将商品添加到redis中
            Sku sku = skuFeign.findById(skuId).getData();//feign远程调用,查询spu信息
            Spu spu = spuFeign.findById(sku.getSpuId()).getData();//feign远程调用,查询sku信息
            //封装OrderItem
            orderItem = this.sku2OrderItem(sku, spu, num);
        }
        //4.将OrderItem添加到redis中
        redisTemplate.boundHashOps(CART + username).put(skuId, orderItem);
    }

    //封装OrderItem
    private OrderItem sku2OrderItem(Sku sku, Spu spu, Integer num) {
        OrderItem orderItem = new OrderItem();
        //spuId
        orderItem.setSpuId(spu.getId());
        //skuId
        orderItem.setSkuId(sku.getId());
        //sku的name
        orderItem.setNum(sku.getNum());
        //sku的单价
        orderItem.setPrice(sku.getPrice());
        //sku数量
        orderItem.setNum(sku.getNum());
        //总价
        orderItem.setMoney(orderItem.getPrice() * num);
        //支付金额
        orderItem.setPayMoney(orderItem.getPrice() * num);
        //图片信息
        orderItem.setImage(sku.getImage());
        //重量
        orderItem.setWeight(sku.getWeight() * num);

        //分类信息
        //一级菜单
        orderItem.setCategoryId1(spu.getCategory1Id());
        //二级菜单
        orderItem.setCategoryId2(spu.getCategory2Id());
        //三级菜单
        orderItem.setCategoryId3(spu.getCategory3Id());
        return orderItem;
    }


    //查看购物车
    @Override
    public Map list(String username) {
        Map map = new HashMap();
        List<OrderItem> orderItemList = redisTemplate.boundHashOps(CART + username).values();
        map.put("orderItemList", orderItemList);
        //封装商品的总数量与总价格
        Integer totalNum = 0;
        Integer totalMoney = 0;
        //遍历计算商品总数量和总价格
        for (OrderItem orderItem : orderItemList) {
            totalNum += orderItem.getNum();//商品总数量
            totalMoney += orderItem.getMoney();//商品总数量
        }
        map.put("totalNum", totalNum);
        map.put("totalMoney", totalMoney);
        return map;
    }
}
